package com.xframe.common.core.dto;


import com.fasterxml.jackson.annotation.JsonProperty;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.Operator;
import com.xframe.xdal.core.util.StringUtil;
import lombok.Data;

@Data
public class ConditionValueDto {
    /**
     * 名称
     */
    private String keyName;

    /**
     * 比较对象
     */
    private String op;

    /**
     * 值
     */
    private Object val;

    /**
     * 组名称
     */
    @JsonProperty("gName")
    private String gName;



    private String esArgs;


    public ConditionValue toConditionValue(){
        ConditionValue conditionValue = null;
        if(!StringUtil.isEmpty(op)) {

            conditionValue = new ConditionValue(keyName, convert(op), val, true);
        }else {
            conditionValue = new ConditionValue(keyName, val);
        }

        if(!StringUtil.isEmpty(gName)) {
            conditionValue.setGroupName(gName);
        }
//        }else {
//            conditionValue.setGroupName(keyName);
//        }
        conditionValue.setEsQueryBuildArgs(esArgs);
        return conditionValue;
    }


    private static Operator convert(String op){
        try {
            return Operator.valueOf(op);
        }catch (Exception exception){
            return Operator.EQ;
        }
    }
}
